/*
 * Copyright (C) 2008 ZXing authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.zxing.journeyapps.barcodescanner;

import com.google.zxing.ResourceTable;
import com.google.zxing.ResultPoint;
import ohos.agp.components.Attr;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.PixelMapHolder;
import ohos.agp.utils.Color;
import ohos.agp.utils.Rect;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.ResourceManager;
import ohos.global.resource.WrongTypeException;
import ohos.media.image.PixelMap;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * This view is overlaid on top of the camera preview. It adds the viewfinder rectangle and partial
 * transparency outside it, as well as the laser scanner animation and result points.
 *
 * @author dswitkin@google.com (Daniel Switkin)
 */
public class ViewfinderView extends Component implements Component.DrawTask {
    protected static final String TAG = ViewfinderView.class.getSimpleName();

    protected static final int[] SCANNER_ALPHA = {0, 64, 128, 192, 255, 192, 128, 64};
    protected static final long ANIMATION_DELAY = 80L;
    protected static final int CURRENT_POINT_OPACITY = 0xA0;
    protected static final int MAX_RESULT_POINTS = 20;
    protected static final int POINT_SIZE = 6;

    protected final Paint paint;
    protected PixelMap resultBitmap;
    protected int maskColor;
    protected  int resultColor;
    protected  int laserColor;
    protected  int resultPointColor;
    protected boolean laserVisibility;
    protected int scannerAlpha;
    protected List<ResultPoint> possibleResultPoints;
    protected List<ResultPoint> lastPossibleResultPoints;
    protected CameraPreview cameraPreview;

    // Cache the framingRect and previewSize, so that we can still draw it after the preview
    // stopped.
    protected Rect framingRect;
    protected Size previewSize;
    protected PixelMapHolder resultBitmapHolder;

    // This constructor is used when the class is built from an XML resource.
    public ViewfinderView(Context context, AttrSet attrs) {
        super(context, attrs);

        // Initialize these once for performance rather than calling them every time in onDraw().
        paint = new Paint();
        paint.setAntiAlias(true);

        ResourceManager resources = getResourceManager();

        // Get setted attributes on view

        try {
            this.maskColor = resources.getElement(ResourceTable.Color_zxing_viewfinder_mask).getColor();
            Optional<Attr> optional = attrs.getAttr("zxing_viewfinder_mask");
            if (optional.isPresent()) {
                this.maskColor = optional.get().getColorValue().getValue();
            }
            optional = attrs.getAttr("zxing_result_view");
            if (optional.isPresent()) {
                this.resultColor = optional.get().getColorValue().getValue();
            } else {
                this.resultColor = resources.getElement(ResourceTable.Color_zxing_result_view).getColor();
            }
            optional = attrs.getAttr("zxing_viewfinder_laser");
            if (optional.isPresent()) {
                this.laserColor = optional.get().getColorValue().getValue();
            } else {
                this.laserColor = resources.getElement(ResourceTable.Color_zxing_viewfinder_laser).getColor();
            }
            optional = attrs.getAttr("zxing_possible_result_points");
            if (optional.isPresent()) {
                this.resultPointColor = optional.get().getColorValue().getValue();
            } else {
                this.resultPointColor = resources.getElement(ResourceTable.Color_zxing_possible_result_points).getColor();
            }
            this.laserVisibility = true;
            optional = attrs.getAttr("zxing_viewfinder_laser_visibility");
            if (optional.isPresent()) {
                this.laserVisibility = optional.get().getBoolValue();
            }
        } catch (IOException | NotExistException | WrongTypeException e) {
            e.printStackTrace();
            resultColor = Color.getIntColor("#b0000000");
            laserColor = Color.getIntColor("#ffcc0000");
            resultPointColor = Color.getIntColor("#c0ffbd21");
        }

        scannerAlpha = 0;
        possibleResultPoints = new ArrayList<>(MAX_RESULT_POINTS);
        lastPossibleResultPoints = new ArrayList<>(MAX_RESULT_POINTS);
        addDrawTask(this);
    }

    public void setCameraPreview(CameraPreview view) {
        this.cameraPreview = view;
        view.addStateListener(new CameraPreview.StateListener() {
            @Override
            public void previewSized() {
                refreshSizes();
                invalidate();
            }

            @Override
            public void previewStarted() {

            }

            @Override
            public void previewStopped() {

            }

            @Override
            public void cameraError(Exception error) {

            }

            @Override
            public void cameraClosed() {

            }
        });
    }

    protected void refreshSizes() {
        if (cameraPreview == null) {
            return;
        }
        Rect framingRect = cameraPreview.getFramingRect();
        Size previewSize = cameraPreview.getPreviewSize();
        if (framingRect != null && previewSize != null) {
            this.framingRect = framingRect;
            this.previewSize = previewSize;
        }
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        refreshSizes();
        if (framingRect == null || previewSize == null) {
            return;
        }
        final Rect frame = framingRect;
        final Size previewSize = this.previewSize;

        final int width = component.getWidth();
        final int height = component.getHeight();

        // Draw the exterior (i.e. outside the framing rect) darkened
        Color color = new Color(resultBitmap != null ? resultColor : maskColor);
        paint.setColor(color);
        canvas.drawRect(new RectFloat(0, 0, width, frame.top), paint);
        canvas.drawRect(new RectFloat(0, frame.top, frame.left, frame.bottom + 1), paint);
        canvas.drawRect(new RectFloat(frame.right + 1, frame.top, width, frame.bottom + 1), paint);
        canvas.drawRect(new RectFloat(0, frame.bottom + 1, width, height), paint);

        if (resultBitmap != null) {
            // Draw the opaque result bitmap over the scanning rectangle
            paint.setAlpha(CURRENT_POINT_OPACITY);

            canvas.drawPixelMapHolderRect(resultBitmapHolder,new RectFloat(frame),paint);
        } else {
            // If wanted, draw a red "laser scanner" line through the middle to show decoding is active
            if (laserVisibility) {
                paint.setColor(new Color(laserColor));

                paint.setAlpha(SCANNER_ALPHA[scannerAlpha]);
                scannerAlpha = (scannerAlpha + 1) % SCANNER_ALPHA.length;

                final int middle = frame.getHeight() / 2 + frame.top;
                canvas.drawRect(new RectFloat(frame.left + 2, middle - 1, frame.right - 1, middle + 2), paint);
            }

            final float scaleX = this.getWidth() / (float) previewSize.width;
            final float scaleY = this.getHeight() / (float) previewSize.height;

            // draw the last possible result points
            if (!lastPossibleResultPoints.isEmpty()) {
                paint.setAlpha(CURRENT_POINT_OPACITY / 2);
                paint.setColor(new Color(resultPointColor));
                float radius = POINT_SIZE / 2.0f;
                for (final ResultPoint point : lastPossibleResultPoints) {
                    canvas.drawCircle(
                             (int) (point.getX() * scaleX),
                             (int) (point.getY() * scaleY),
                            radius, paint
                    );
                }
                lastPossibleResultPoints.clear();
            }

            // draw current possible result points
            if (!possibleResultPoints.isEmpty()) {
                paint.setAlpha(CURRENT_POINT_OPACITY);
                paint.setColor(new Color(resultPointColor));
                for (final ResultPoint point : possibleResultPoints) {
                    canvas.drawCircle(
                            (int) (point.getX() * scaleX),
                            (int) (point.getY() * scaleY),
                            POINT_SIZE, paint
                    );
                }

                // swap and clear buffers
                final List<ResultPoint> temp = possibleResultPoints;
                possibleResultPoints = lastPossibleResultPoints;
                lastPossibleResultPoints = temp;
                possibleResultPoints.clear();
            }

            // TODO 局部绘制，不断更新扫码框的扫描线
            // Request another update at the animation interval, but only repaint the laser line,
            // not the entire viewfinder mask.
            /*postInvalidateDelayed(ANIMATION_DELAY,
                    frame.left - POINT_SIZE,
                    frame.top - POINT_SIZE,
                    frame.right + POINT_SIZE,
                    frame.bottom + POINT_SIZE);*/
        }
    }

    public void drawViewfinder() {
        PixelMap resultBitmap = this.resultBitmap;
        resultBitmapHolder = new PixelMapHolder(resultBitmap);
        this.resultBitmap = null;
        if (resultBitmap != null) {
            resultBitmap.release();
        }
        invalidate();
    }

    /**
     * Draw a PixelMap with the result points highlighted instead of the live scanning display.
     *
     * @param result An image of the result.
     */
    public void drawResultBitmap(PixelMap result) {
        resultBitmap = result;
        invalidate();
    }

    /**
     * Only call from the UI thread.
     *
     * @param point a point to draw, relative to the preview frame
     */
    public void addPossibleResultPoint(ResultPoint point) {
        if (possibleResultPoints.size() < MAX_RESULT_POINTS)
            possibleResultPoints.add(point);
    }

    public void setMaskColor(int maskColor) {
        this.maskColor = maskColor;
    }

    public void setLaserVisibility(boolean visible) {
        this.laserVisibility = visible;
    }

}
